package com.ocm.commerceinteraction.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ocm.commerceinteraction.GlobalApplication;
import com.ocm.commerceinteraction.R;
import com.ocm.commerceinteraction.config.RequestAddr;
import com.ocm.commerceinteraction.model.GroupDatas;
import com.ocm.commerceinteraction.model.GroupDetail;
import com.ocm.commerceinteraction.util.helper.LogHelper;
import com.ocm.commerceinteraction.util.view.RoundImageView;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Map;

/**
 * Created by Curry on 2016-4-22.
 */
public class GroupListViewAdapter extends BaseAdapter {
    //    private GroupDatas data;
    private LayoutInflater layoutInflater;
    private Context context;
    private GroupDetail groupDetail;

    public GroupListViewAdapter(Context context, GroupDetail groupDetail) {
        this.context = context;
//        this.data = data;
        this.groupDetail = groupDetail;
        this.layoutInflater = LayoutInflater.from(context);
    }


    public final class ViewHolder {
        public RoundImageView headImg;
        public TextView title;
        public TextView info;
        public TextView peopelNum;
        public TextView tip;
        public TextView unread_msg_number;
    }

    @Override
    public int getCount() {
        return groupDetail.getData().size();
    }

    /**
     * 获得某一位置的数据
     */
    @Override
    public Object getItem(int position) {
        return groupDetail.getData().get(position);
    }

    /**
     * 获得唯一标识
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GroupDatas datas = null;
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            //获得组件，实例化组件
                convertView = layoutInflater.inflate(R.layout.chat_item, null);
            holder.headImg = (RoundImageView) convertView.findViewById(R.id.head);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.info = (TextView) convertView.findViewById(R.id.des);
            holder.peopelNum = (TextView) convertView.findViewById(R.id.tv_percen_num);
            holder.tip = (TextView) convertView.findViewById(R.id.tip);
            holder.unread_msg_number = (TextView) convertView.findViewById(R.id.unread_msg_number);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (groupDetail.getData().get(position).getFlag() == 1){
            holder.tip.setVisibility(View.VISIBLE);
        }else {
            holder.tip.setVisibility(View.GONE);
        }

        holder.title.setText(groupDetail.getData().get(position).getGroupname());
        holder.info.setText(groupDetail.getData().get(position).getDescscription());
        holder.peopelNum.setText(groupDetail.getData().get(position).getCurrent_count() + "/" +
                groupDetail.getData().get(position).getMaxusers());
        if (GlobalApplication.unReadListGroup.get(groupDetail.getData().get(position).getHuanxing_groupid()) != null) {
            holder.unread_msg_number.setText(GlobalApplication.unReadListGroup.get(groupDetail.getData().get(position).getHuanxing_groupid())+"");
            holder.unread_msg_number.setVisibility(View.VISIBLE);
        } else {
            holder.unread_msg_number.setVisibility(View.INVISIBLE);
        }

        Picasso.with(context).load(RequestAddr.baseUrl + groupDetail.getData().get(position).getIcon()).into(holder.headImg);


        return convertView;
    }


}
